package cn.shujuhai.workflow.controller.flow;

import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.workflow.service.flow.FlowableService;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: ZhangDeZhong
 * @Date: 2025/3/18 16:19
 * @Description:
 **/

@RestController
@RequestMapping("/workflow/flowable")
@RequiredArgsConstructor
public class FlowableController {

    private final FlowableService flowableService;

    /**
     * 分页查询流程定义列表
     */
    @GetMapping("/processList")
    public ResultVo<?> getProcessList(Long departmentId) {
        return this.flowableService.getProcessList(departmentId);
    }


    /**
     * 查询流程定义流程图
     */
    @PostMapping("/getProcessDiagram")
    public ResultVo<?> getProcessDiagram(@RequestParam String processDefinitionId) {
        return this.flowableService.getProcessDiagram(processDefinitionId);
    }


    /**
     * 保存 BPMN 流程定义
     */
    @PostMapping("/saveBpmn")
    public ResultVo<?> saveBpmn(@RequestBody Map<String, Object> data) {
        return this.flowableService.saveBpmn(data);
    }

    @PostMapping("/deployProcess")
    public ResultVo<?> deployProcess(@RequestParam String bpmnFileName) {
        return this.flowableService.deployProcess(bpmnFileName);
    }


    //删除流程定义
    @PostMapping("/deleteProcessDefinition")
    public ResultVo<?> deleteProcessDefinition(@RequestParam String deploymentId, @RequestParam boolean cascade) {
        return this.flowableService.deleteProcessDefinition(deploymentId, cascade);
    }

    // 查询流程实例列表
    @GetMapping("/processInstanceList")
    public ResultVo<?> getProcessInstanceList(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "10") int pageSize) {
        return flowableService.getProcessInstanceList(pageNum, pageSize);
    }

    // 查询历史流程实例列表
    @GetMapping("/getHistoryProcessInstanceList")
    public ResultVo<?> getHistoryProcessInstanceList(@RequestParam(defaultValue = "1") int pageNum,
                                                     @RequestParam(defaultValue = "10") int pageSize) {
        return flowableService.getHistoryProcessInstanceList(pageNum, pageSize);
    }

    // 查询任务列表
    @GetMapping("/tasks")
    public ResultVo<?> getTasks(@RequestParam String processInstanceId) {
        return flowableService.getTasks(processInstanceId);
    }


    // 查询任务列表
    @GetMapping("/getTaskList")
    public ResultVo<?> getTaskList() {
        return flowableService.getTaskList();
    }


    // 查询任务列表
    @GetMapping("/getTaskFinishList")
    public ResultVo<?> getTaskFinishList() {
        return flowableService.getTaskFinishList();
    }

    @GetMapping(value = "/process/diagram")
    public void processDiagram(@RequestParam("processInstanceId") String processInstanceId, HttpServletResponse response) throws Exception {
        this.flowableService.processDiagram(processInstanceId, response);
    }


    @GetMapping("/getFormDataByProcessInstanceId")
    public ResultVo<?> getFormDataByProcessInstanceId(@RequestParam String processInstanceId) throws JsonProcessingException {
        return this.flowableService.getFormDataByProcessInstanceId(processInstanceId);
    }

    //我的提交
    @GetMapping("/processInstanceList1")
    public ResultVo<?> getProcessInstanceLists(@RequestParam(defaultValue = "1") int pageNum,
                                               @RequestParam(defaultValue = "10") int pageSize) {
        return flowableService.getProcessInstanceLists(pageNum, pageSize);
    }

    // 我的历史
    @GetMapping("/getHistoryProcessInstanceList1")
    public ResultVo<?> getHistoryProcessInstanceLists(@RequestParam(defaultValue = "1") int pageNum,
                                                      @RequestParam(defaultValue = "10") int pageSize) {
        return flowableService.getHistoryProcessInstanceLists(pageNum, pageSize);
    }


}
